<?php
print_r($this->data);
?>
<div class="page-header">
    <h1>
        Form Pembelian
        <small>
            <i class="icon-double-angle-right"></i>
            Pembelian Voucher Seluler/PLN
        </small>
    </h1>
</div>

<?php
if ($this->msg) {
    ?>
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>

        <strong>
            <i class="icon-remove"></i>
            Error
        </strong>

        <?php echo $this->msg; ?>
        <br />
    </div>
    <?php
}
    ?>

    <div class="row">
        <div class="col-xs-11">
            <form id="formBuy" class="form-horizontal" role="form" action="" method="post">
                
                <!-- fromAcc input-->
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right">Dari Rekening</label>
                        <div class="col-sm-9">
                        <select id="fromAcc" name="fromAcc" class="col-xs-10 col-sm-5">
                            <?php

                           foreach ($this->accountList as $oAcc) {
                                    echo "<option value='".$oAcc['bank_account_no']."'>". $oAcc['bank_name'] .' '. $oAcc['bank_account_no'] ." (". $oAcc['account_name'] .")</option>";
                           }
                           ?>
                        </select>
                        </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right">Operator</label>
                    <div class="col-sm-9">
                    <select id="slOperator" name="slOperator" class="col-xs-10 col-sm-5" required>
                           <option value=''>== Pilih Operator ==</option>
                           <?php
                           foreach ($this->op as $ops){

                           echo "<option value='".$ops['pulsa_operator']."'>". $ops['pulsa_operator'] ."</option>";

                           }
                           ?>
                        </select>
                    </div>
                </div>

                <!-- voucherCode input-->
                <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">Voucher</label>
                        <div class="col-sm-9">
                        <select id="voucherCode" name="voucherCode" class="col-xs-10 col-sm-5" required>
                           <option value=''>== Pilih Voucher ==</option>
                        </select>
                        </div>
                </div>

                <!-- no_celluler input-->
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right">Nomor Seluler/PLN</label>
                    <div class="col-sm-9">
                    <input id="no_celluler" name="no_celluler" type="text" placeholder="Nomor Seluler/PLN"
                    class="col-xs-10 col-sm-5" required>
                    <br/><br/>
                    <p class="help-block">Pastikan Nomor Seluler/PLN yang Anda masukan benar. Transaksi yang telah dikirim tidak dapat dibatalkan.</p>
                    </div>
                </div>

                <!-- remark input-->
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right">Catatan</label>
                    <div class="col-sm-9">
                        <textarea id="remark" name="remark" type="text" placeholder="Catatan"
                        class="col-xs-10 col-sm-5"></textarea>
                    </div>
                </div>

            <div class="clearfix form-actions">
                <div class="col-md-offset-3 col-md-9">
                    <button class="btn btn-success" type="submit">
                        <i class="icon-ok bigger-110"></i>
                        Submit
                    </button>

                    &nbsp; &nbsp; &nbsp;
                    <button class="btn" type="reset">
                        <i class="icon-undo bigger-110"></i>
                        Reset
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>


 <script>

      $("select#slOperator").change(function(){
         var op = $("#slOperator").val();
         var options = '';
         options += '<option value="">== Loading Voucher ==</option>';
         $("select#voucherCode").html(options);
         $("select#voucherCode").attr('disabled','disabled');

         $.getJSON("<?php echo $this->url() ?>/jsondata/vouchercode",{slOperator: op, ajax: 'true'}, function(j){
              options = '';
              options += '<option value="">== Pilih Voucher ==</option>';
              for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].pulsa_code + '">' + j[i].pulsa_desc + '</option>';
              }
              $("select#voucherCode").removeAttr('disabled');
              $("select#voucherCode").html(options);
        })
      });


    /* Attach a submit handler to the form */
    $("#frm").submit(function(event) {

        /* Stop form from submitting normally */
        event.preventDefault();

        /* Clear result div*/
        $("#div_result").html('');
        $("#btnSubmit").html('Memproses...');
        $("#btnSubmit").attr('disabled','disabled');

        /* Get some values from elements on the page: */
        var values = $(this).serialize();

        var msg = [];

        /* Send the data using post and put the results in a div */

        $.ajax({
            url: "<?php echo $this->url() ?>/purchase/cellphonebuy/",
            type: "post",
            data: values,
            success: function(response, opts){
                //var cekSess = response.toUpperCase().search("SESSION TIMEOUT");

                //if (cekSess < 0){
                    var cekStatus = response.toUpperCase().search("ERROR");

                    if (cekStatus < 0){
                        $("#div_content").html(response);
                        location.href='#div_content';
                    }else{

                        var resp = response.replace('ERROR', '');

                        msg.push('<span>'+resp+'</span>');
                        $.messager.alert('Info', msg.join('<br/>'));

                        //$("#div_result").html(resp);
                        $("#btnSubmit").removeAttr('disabled');
                        $("#btnSubmit").html('Submit');
                    }
                /*}else{
                    location.href='<?php echo $this->url() ?>/index/login/msg/Session Timeout silahkan login kembali !';
                }*/
            },
            error:function(response, opts){
                //alert("failure");
                msg.push('<span>'+response+'</span>');
                $.messager.alert('Info', msg.join('<br/>'));

                $("#btnSubmit").removeAttr('disabled');
                $("#btnSubmit").html('Submit');
            }
        });

    });

    </script>